#include <interfacelib/window/loadcontrol.h>
#include "Dialog/MakeChannelDialog.h"
#include "Define/control.h"
#include <interfacelib/window/button.h>
#include "Network/NetworkManager.h"
#include <netlib/packet/packetpool.h>
#include "../../common/protocol.h"


MakeChannelDialog::MakeChannelDialog() : m_editChannelName( NULL )
{
	ADD_USERFUNCTION( "OnClickMakeChannelOK", onMakeChannelOKClick );
	ADD_USERFUNCTION( "OnClickMakeChannelCancel", onMakeChannelCancelClick );
}

MakeChannelDialog::~MakeChannelDialog()
{
}

void MakeChannelDialog::initialize()
{
	m_editChannelName->clear();
}

bool MakeChannelDialog::create( interfacelib::Window::LoadControl *loadControl )
{
	interfacelib::Window::LoadControl::ControlProperty *property = loadControl->getControlProperty( SCRIPT_MAKE_CHANNEL_DIALOG_NAME );
	if ( property )
		Panel::create( property->resourceName, property->pos.x, property->pos.y , property->attribute );

	loadControl->setLinkFunctionPanel( this );
	loadControl->loadChild( SCRIPT_MAKE_CHANNEL_DIALOG_NAME, this );

	m_editChannelName = ( interfacelib::Window::EditBox * )loadControl->getControl( "ChannelName", interfacelib::Window::Window::WindowType::EDITBOX );
	return ( true );
}

void MakeChannelDialog::onMakeChannelOKClick()
{
	SYNC_GET_PACKET( syncGetPacket, makeChannelPacket );
	makeMakeChannelPacketReq( makeChannelPacket, ( char *)m_editChannelName->getBuffer() );

	NetworkManager::getInstance()->send( makeChannelPacket );
}

void MakeChannelDialog::onMakeChannelCancelClick()
{
	setVisible( false );
}

bool MakeChannelDialog::makeMakeChannelPacketReq( netlib::packet::Packet *packet, char *channelName )
{
	if ( !channelName ) return ( false );
	packet->ID( protocol::community::CreateChannelReq );
	*packet << channelName;

	return ( true );
}